package com.ruoyi.sea.service.impl;

import java.net.Socket;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.sea.mapper.SeaDeviceMapper;
import com.ruoyi.sea.domain.SeaDevice;
import com.ruoyi.sea.service.ISeaDeviceService;

/**
 * 海洋设备Service业务层处理
 * 
 * @author lwq
 * @date 2025-04-02
 */
@Service
@Slf4j
public class SeaDeviceServiceImpl implements ISeaDeviceService 
{
    @Autowired
    private SeaDeviceMapper seaDeviceMapper;

    /**
     * 查询海洋设备
     * 
     * @param id 海洋设备主键
     * @return 海洋设备
     */
    @Override
    public SeaDevice selectSeaDeviceById(Long id)
    {
        return seaDeviceMapper.selectSeaDeviceById(id);
    }

    /**
     * 查询海洋设备列表
     * 
     * @param seaDevice 海洋设备
     * @return 海洋设备
     */
    @Override
    public List<SeaDevice> selectSeaDeviceList(SeaDevice seaDevice)
    {

        return seaDeviceMapper.selectSeaDeviceList(seaDevice);
    }

    /**
     * 新增海洋设备
     * 
     * @param seaDevice 海洋设备
     * @return 结果
     */
    @Override
    public int insertSeaDevice(SeaDevice seaDevice)
    {
        seaDevice.setCreateTime(DateUtils.getNowDate());
        return seaDeviceMapper.insertSeaDevice(seaDevice);
    }

    /**
     * 修改海洋设备
     * 
     * @param seaDevice 海洋设备
     * @return 结果
     */
    @Override
    public int updateSeaDevice(SeaDevice seaDevice)
    {
        seaDevice.setUpdateTime(DateUtils.getNowDate());
        return seaDeviceMapper.updateSeaDevice(seaDevice);
    }

    /**
     * 批量删除海洋设备
     * 
     * @param ids 需要删除的海洋设备主键
     * @return 结果
     */
    @Override
    public int deleteSeaDeviceByIds(Long[] ids)
    {
        return seaDeviceMapper.deleteSeaDeviceByIds(ids);
    }

    /**
     * 删除海洋设备信息
     * 
     * @param id 海洋设备主键
     * @return 结果
     */
    @Override
    public int deleteSeaDeviceById(Long id)
    {
        return seaDeviceMapper.deleteSeaDeviceById(id);
    }

    /**
     * 定时任务 连接TCP服务器, 连接成功则更新设备状态
     */


//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void updateDeviceStatus(){
//        List<SeaDevice> deviceList = seaDeviceMapper.selectSeaDeviceList(new SeaDevice());
//        String tcpHost = "127.0.0.1";
//        int port = 5000;
//        Socket socket = null;
//        if (deviceList != null && !deviceList.isEmpty()){
//            for (SeaDevice device : deviceList) {
//                String oldStatus = device.getStatus();
//                tcpHost = device.getRemark().trim();
//                SeaDevice seaDevice = new SeaDevice();
//                try {
//                    socket = new Socket(tcpHost, port);
//                    if (socket.isConnected()) {
//                        log.info(tcpHost + "tcp 连接成功");
//                        seaDevice.setId(device.getId());
//                        seaDevice.setStatus("1");
//                    } else {
//                        seaDevice.setId(device.getId());
//                        seaDevice.setStatus("0");
//                    }
//                } catch (Exception e) {
//                    log.warn(tcpHost + "tcp 连接失败");
//                    seaDevice.setStatus("0");
//                    seaDevice.setId(device.getId());
//                } finally {
//                    if (!oldStatus.equals(seaDevice.getStatus())) {
//                        try {
//                            seaDeviceMapper.updateSeaDevice(seaDevice);
//                            socket.close();
//                            System.out.println(tcpHost + "关闭socket成功");
//                        } catch (Exception e) {
//                            System.out.println("关闭socket失败");
//                        }
//                    }
//                }
//            }
//        }
//    }
}
